package com.woniu.exception;


import com.woniu.exception.gateway.GatewayException;
import cn.hutool.json.JSONUtil;
import com.woniu.exception.meals.*;
import com.woniu.exception.user.UserException;
import com.woniu.utils.Result;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.*;

/**
 * @author jiang
 */
@RestControllerAdvice
public class ExceptionHandle {
    @ExceptionHandler(UserException.class)
    public Result handlerException(UserException u) {
        System.out.println("这是一段被抓取的错误--User");
        u.printStackTrace();
        return Result.getResult().setCode(u.getCode()).setMsg(u.getMessage());
    }

    //
    @ExceptionHandler(GatewayException.class)
    public Result gatewayException(GatewayException g) {
        System.out.println("这是一段被抓取的错误--User");
        g.printStackTrace();
        return Result.getResult().setCode(g.getCode()).setMsg(g.getMessage());
    }

    @ExceptionHandler(XinanException.class)
    public Result<?> XinanException(XinanException e) {
        return Result.getResult().setCode(e.getCode()).setMsg(e.getMessage());
    }

    //    @ExceptionHandler(OrderException.class)
//    public Result<?> orderException(OrderException e) {
//        return Result.getResult(String.class).setCode(e.getCode()).setMsg(e.getMsg());
//    }
//    @ExceptionHandler(ProductException.class)
//    public Result<?> ProductException(ProductException e) {
//        return Result.getResult(String.class).setCode(e.getCode()).setMsg(e.getMsg());
//    }
//    @ExceptionHandler(StoreException.class)
//    public Result<?> StoreException(StoreException e) {
//        return Result.getResult(String.class).setCode(e.getCode()).setMsg(e.getMsg());
//    }
//    @ExceptionHandler(MealsException.class)
//    public Result<?> MealsException(MealsException e) {
//        return Result.getResult(String.class).setCode(e.getCode()).setMsg(e.getMsg());
//    }
//    @ExceptionHandler(FeignException.class)
//    public Result<?> FeignException(FeignException e) {
//        return Result.getResult(String.class).setCode(e.getCode()).setMsg(e.getMsg());
//    }
//    @ExceptionHandler(CommentException.class)
//    public Result<?> CommentException(CommentException e) {
//        return Result.getResult(String.class).setCode(e.getCode()).setMsg(e.getMsg());
//    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    /**
     * 处理所有RequestBody注解参数验证异常
     */
    public Result<?> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        return Result.getResult(String.class).setCode(HttpStatus.BAD_REQUEST.value()).setMsg(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * 处理所有RequestParam注解数据验证异常
     */
    @ExceptionHandler({BindException.class})
    public Result<?> handleBindException(BindException e) {
        List<ObjectError> allErrors = e.getAllErrors();
        return Result.getResult(String.class).setCode(HttpStatus.BAD_REQUEST.value()).setMsg(allErrors.get(0).getDefaultMessage());
    }

    /**
     * 当@Validated修饰类时，处理接口原子类型参数没有通过校验的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler({ConstraintViolationException.class})
    public Result<?> handleConstraintViolationException(ConstraintViolationException e) {
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator();
        ConstraintViolation<?> next = iterator.next();
        return Result.getResult(String.class).setCode(HttpStatus.BAD_REQUEST.value()).setMsg(next.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result<?> exception(Exception e) {
        e.printStackTrace();
        return Result.getResult(String.class).setCode(500).setMsg(e.getMessage());
    }
}
